﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GamePlugin.Logics;
using GamePlugin.Logics.Core;
using GamePlugin.Logics.Data;
using System.Diagnostics;
using Microsoft.Win32;
using GamePlugin.IO;
using System.Windows.Forms;

namespace GamePlugin.Controls
{
    /// <summary>
    /// Interaction logic for PropertyList.xaml
    /// </summary>
    public partial class PropertyList : System.Windows.Controls.UserControl
    {
        public PropertyItemList _PropertyItemList;

        public PropertyList()
        {
            InitializeComponent();

            this.Init();
        }

        public void Init()
        {
            detailedTreeView.DataContext = Workspace.Instance.PropertyItemList.DetailedPropertyTreeRoot;
            propertyTreeView.DataContext = Workspace.Instance.PropertyItemList.PropertyTreeRoot;
            LengnedItemListBox.DataContext = Workspace.Instance.LengendItemList;
            _PropertyItemList = Workspace.Instance.PropertyItemList;
        }

        public void UpdatePropertyItemListBoxFromPropertyItem(PropertyItem item)
        {
            DetailedPropertyTreeItem root = Workspace.Instance.PropertyItemList.DetailedPropertyTreeRoot;
            root.ClearAll();

            for (int i = 0; i < item.Children.Count; i++)
            {
                string value = item.Children[i];
                string[] values = value.Split('=');
                Debug.Assert(values.Length == 2);
                root.Enable(values[1]);
            }
        }

        public void UpdatePropertyItemListBoxFromDetailedProperty()
        {
            // get all properties
            PropertyItem propertyItem = Helper.GetMainWindow().propertyList1.PropertyItemListBox.DataContext as PropertyItem;
            if (propertyItem != null)
            {
                propertyItem.Children.Clear();
                List<string> current = Workspace.Instance.PropertyItemList.DetailedPropertyTreeRoot.GetList();
                for (int i = 0; i < current.Count; i++)
                    propertyItem.Children.Add(i.ToString() + "=" + current[i]);
            }

            Helper.GetMainWindow().searchInformation.UpdateCombox();
        }

        private void TreeView_Selected(object sender, RoutedEventArgs e)
        {
            PropertyTreeItem item = null;
            var tvi = e.OriginalSource as TreeViewItem;
            if (tvi != null)
                item = tvi.DataContext as PropertyTreeItem;
            if (item != null && item.IsLeaf == true)
            {
                PropertyItem propertyItem = _PropertyItemList.GetPropertyItem(item.FullName);
                PropertyItemListBox.DataContext = propertyItem;
                this.UpdatePropertyItemListBoxFromPropertyItem(propertyItem);
            }
        }

        private void ModifyData_Click(object sender, RoutedEventArgs e)
        {
            this.UpdatePropertyItemListBoxFromDetailedProperty();
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            if (LegendItemNameText.Text != null && LegendItemNameText.Text.Length > 0)
            {
                if (Workspace.Instance.LengendItemList.Contain(LegendItemNameText.Text) == false)
                {
                    Workspace.Instance.LengendItemList.Children.Add(LegendItemNameText.Text);
                }
            }
            Workspace.Instance.LengendItemList.Sort();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (LengnedItemListBox.SelectedValue != null)
                Workspace.Instance.LengendItemList.Children.Remove(LengnedItemListBox.SelectedValue as string);
            Workspace.Instance.LengendItemList.Sort();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog1.InitialDirectory = IOManager.Instance.DataFolderPath;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = "DefaultProperty.prp";
            saveFileDialog1.Filter = "Property File|*.prp";
            saveFileDialog1.Title = "Save property data";
            ;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog1.FileName == null || saveFileDialog1.FileName.Length == 0)
                    return;
                //IOManager.Instance.SaveSearchList(searchListBox.DataContext as SearchInfoItemList, saveFileDialog1.FileName);
                IOManager.Instance.SavePropertyData(Workspace.Instance.LengendItemList, Workspace.Instance.PropertyItemList, saveFileDialog1.FileName);
            }
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.InitialDirectory = IOManager.Instance.DataFolderPath;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FileName = "DefaultProperty.prp";
            openFileDialog.Filter = "Property File|*.prp";
            openFileDialog.Title = "Load property data";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog.FileName == null || openFileDialog.FileName.Length == 0)
                    return;
                IOManager.Instance.LoadPropertyData(Workspace.Instance.LengendItemList, Workspace.Instance.PropertyItemList, openFileDialog.FileName);
            }

            if (propertyTreeView.SelectedItem != null)
            {
                PropertyItem propertyItem = _PropertyItemList.GetPropertyItem((propertyTreeView.SelectedItem as PropertyTreeItem).FullName);
                PropertyItemListBox.DataContext = propertyItem;
                this.UpdatePropertyItemListBoxFromPropertyItem(propertyItem);

                this.UpdatePropertyItemListBoxFromPropertyItem(propertyItem);
            }
        }
    }
}
